import axios from 'axios'

//创建axios实例
const service = axios.create({
  baseURL: 'http://localhost:8888', //api的baseurl
  timeout: 5000, //请求超时时间
})

// request拦截器
service.interceptors.request.use(config => {
  // Do something before request is sent
  if (localStorage.getItem("JWT_TOKEN")) {
    config.headers['JWT_TOKEN'] = localStorage.getItem("JWT_TOKEN") // 让每个请求携带token--['JWT_TOKEN']为自定义key 请根据实际情况自行修改
    console.log(config.headers['JWT_TOKEN'])
  } else {
    console.log("localstorage没有token");
  }
  return config
}, error => {
  // Do something with request error
  console.log(error) // for debug
  Promise.reject(error)
})


axios.interceptors.response.use(
  response => {
    console.log("response进来了")
    const data = response.data
    if (data.code === 11) {
      // 登录过期 删除本地存储当中的token
      // localStorage.removeItem('token')
      // localStorage.removeItem('userInfo')
      // 跳转到login页面
      // window.location.href = '/login' // 必须用这个方法 ,否则  userInfo 显示为空
    }
    return response
  }
)
// 导出
export default service;